package com.qc.wintrax.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.adapter.MsgAdapter;
import com.qc.wintrax.model.Msg;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatActivity extends BaseActivity {

    @Bind(R.id.msgListView)
    ListView msgListView;
    @Bind(R.id.receive_view)
    TextView receiveView;
    @Bind(R.id.inputText)
    EditText inputText;
    @Bind(R.id.send_view)
    TextView sendView;
    private List<Msg> msgList = new ArrayList<Msg>();
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        setChat();
    }


    public void setChat() {
        initMsgs();
        adapter = new MsgAdapter(context, R.layout.msg_item, msgList);
        msgListView.setAdapter(adapter);
    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello, how are you?", Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Fine, thank you, and you?", Msg.TYPE_SEND);
        msgList.add(msg2);
        Msg msg3 = new Msg("I am fine, too!", Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    @OnClick({R.id.receive_view, R.id.send_view})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.receive_view:
                String content1 = inputText.getText().toString();
                if (!"".equals(content1)) {
                    Msg msg = new Msg(content1, Msg.TYPE_RECEIVED);
                    msgList.add(msg);
                    adapter.notifyDataSetChanged();
                    msgListView.setSelection(msgList.size());
                    inputText.setText("");
                }
                break;
            case R.id.send_view:
                String content = inputText.getText().toString();
                if (!"".equals(content)) {
                    Msg msg = new Msg(content, Msg.TYPE_SEND);
                    msgList.add(msg);
                    adapter.notifyDataSetChanged();
                    msgListView.setSelection(msgList.size());
                    inputText.setText("");
                }
                break;
        }
    }
}
